package de.wittekind.bjs.frontend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;
import de.wittekind.bundesjugendspiele.server.interfaces.Try;
/**
 * @author Fynn Hauptmeier
 */
public class EditView {
	private Schueler schueler;
	private Entry[][] entries;
	private String[] titles;
	private boolean active;

	public EditView(Schueler choosedSchueler, Discipline activeDiscipline)
			throws IOException {
		schueler = choosedSchueler;
		Map<Discipline, List<Entry>> entries = new HashMap<Discipline, List<Entry>>();
		if (schueler != null && activeDiscipline != null) {
			boolean hasFocus = false;
			for (Try tr : schueler.getTries()) {
				Discipline dis = tr.getDiscipline();
				if (!entries.containsKey(dis)) {
					entries.put(dis, new ArrayList<Entry>());
				}
				List<Entry> disEntries = entries.get(dis);
				Entry entry = null;
				if (activeDiscipline.hasDiscipline(dis)) {
					if (hasFocus) {
						entry = new Entry(tr, true);
					} else {
						entry = new Entry(tr, true, true);
					}
				} else {
					entry = new Entry(tr, false);
				}
				disEntries.add(entry);
			}
			List<Entry[]> entriesList = new ArrayList<Entry[]>();
			List<String> titlesList = new ArrayList<String>();
			Discipline activeD = null;
			for (Discipline e : entries.keySet()) {
				if (activeDiscipline.hasDiscipline(e)) {
					activeD = e;
				}
			}
			if (activeD != null) {
				Entry[] actEntryAr = new Entry[entries.get(activeD).size()];
				entries.get(activeD).toArray(actEntryAr);
				entriesList.add(actEntryAr);
				titlesList.add(activeD.toString());
			}

			for (Discipline dis : entries.keySet()) {

				if (!dis.equals(activeD)) {

					Entry[] entryAr = new Entry[entries.get(dis).size()];
					entries.get(dis).toArray(entryAr);
					entriesList.add(entryAr);
					titlesList.add(dis.toString());
				}

			}
			this.entries = new Entry[entriesList.size()][];
			// System.out.println(titlesList.get(0) + titlesList.get(1)
			// + titlesList.get(2) + titlesList.get(3));
			entriesList.toArray(this.entries);
			this.titles = new String[titlesList.size()];
			titlesList.toArray(this.titles);
			active = true;
		} else {
			active = false;
		}
	}

	public Entry[] getFirstEntries() {
		if (entries != null && entries.length > 0)
			return entries[0];
		return null;
	}

	public List<Entry> getFirstList() {
		if (entries != null && entries.length > 0) {
			List<Entry> list = new ArrayList<Entry>(entries[0].length);
			for (Entry e : entries[0]) {
				list.add(e);
			}
			return list;
		}
		return null;
	}

	public Entry[] getSecondEntries() {
		if (entries != null && entries.length > 1)
			return entries[1];
		return null;
	}

	public Entry[] getThirdEntries() {
		if (entries != null && entries.length > 2)
			return entries[2];
		return null;
	}

	public Entry[] getFourthEntries() {
		if (entries != null && entries.length > 3)
			return entries[3];
		return null;
	}

	public String getFirstTitle() {
		if (titles != null && titles.length > 0)
			return titles[0];
		return null;
	}

	public String getSecondTitle() {
		if (titles != null && titles.length > 1)
			return titles[1];
		return null;
	}

	public String getThirdTitle() {
		if (titles != null && titles.length > 2)
			return titles[2];
		return null;
	}

	public String getFourthTitle() {
		if (titles != null && titles.length > 3)
			return titles[3];
		return null;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Entry[][] getEntries() {
		return entries;
	}

	public boolean isRendered() {
		return entries != null;
	}
}
